package com.software.dao;


import com.software.model.Admin;
import com.software.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDAO {
    /**
     * 通过用户名获取管理员信息
     * @param username 管理员用户名
     * @return 如果存在则返回Admin对象，否则返回null
     */
    public Admin getAdminByUsername(String username) {
        String sql = "SELECT * FROM admin WHERE username = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);

            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    // 获取数据库中的管理员信息
                    String adminId = rs.getString("admin_id");
                    String password = rs.getString("password");
                    String role = rs.getString("role");

                    // 创建并返回Admin对象
                    return new Admin(adminId, username, password, role);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
